package com.example.fund.updatestategy;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.fund.util.DBManager;

public abstract class AbstractUpdateStrategy implements UpdateStrategy
{
	protected String number;
	protected AbstractUpdateStrategy(String number)
	{
		this.number = number;
	}
	
	@Override
	public boolean isNeedUpdate() 
	{
		if("0000001".equals(number))
		{
			return true;
		}
		
		if(number==null || number.trim().length()==0)
		{
			return false;
		}
		
		SQLiteDatabase db = DBManager.getDB();

		String sql = "select min(date) date from FundNetValue where fundnumber='"+number+"'";
		Cursor c = db.rawQuery(sql, null);
		String date = null;
		if (c.moveToNext())
		{
            date = c.getString(c.getColumnIndex("date"));
		}
		c.close();
		
        if(date==null || date.trim().length()==0)
        {
        	return true;
        }
        
        StringBuffer sb = new StringBuffer();
        sb.append("select * from FundNetValue ");
        sb.append("where fundnumber='0000001' ");
        sb.append("and date not in(select date from FundNetValue where fundnumber='"+number+"' ) ");
        sb.append("and date>'"+date+"' ");
		String sql1 = sb.toString();
		Cursor c1 = db.rawQuery(sql1, null);
		boolean isNeed = false;
		if (c1.moveToNext())
		{
			isNeed = true;
		}
		c.close();
        
		return isNeed;
	}
	
	/**
	 * 获取最新的日期
	 * @param activity
	 * @param number
	 * @return
	 */
	public String getFundMaxDate()
	{
		SQLiteDatabase db = DBManager.getDB();

		String maxDate = "";
		
		String sqlQuery = "select max(date) lastDate from FundNetValue where fundNumber='"+number+"'";
		Cursor c = db.rawQuery(sqlQuery, null);
		if (c.moveToNext())
		{
			String date = c.getString(c.getColumnIndex("lastDate"));
			if(date!=null)
			{
				maxDate = date;
			}
		}
		c.close();
		return maxDate;
	}
}
